package com.syxd.controller;

import com.syxd.common.annotation.LogOperation;
import com.syxd.common.constant.Constant;
import com.syxd.common.entity.WebResult;
import com.syxd.common.utils.AssertUtils;
import com.syxd.common.utils.ExcelUtils;
import com.syxd.common.utils.PageData;
import com.syxd.dto.SysRolePermissionDTO;
import com.syxd.excel.SysRolePermissionExcel;
import com.syxd.service.SysRolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 *
 *
 * @author hr tecsmile@outlook.com
 * @since 1.0.0 2019-07-15
 */
@RestController
@RequestMapping("sys/sysrolepermission")
@Api(tags="角色权限关联表")
public class SysRolePermissionController {
    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")
    })
    @LogOperation(apiType = "sysrolepermission", apiPath = "sys/sysrolepermission/page")
    @RequiresPermissions("sys:sysrolepermission:page")
    public WebResult<PageData<SysRolePermissionDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<SysRolePermissionDTO> page = sysRolePermissionService.page(params);

        return new WebResult<PageData<SysRolePermissionDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @LogOperation(apiType = "sysrolepermission", apiPath = "sys/sysrolepermission/{id}")
    @RequiresPermissions("sys:sysrolepermission:info")
    public WebResult<SysRolePermissionDTO> get(@PathVariable("id") Long id) {
        SysRolePermissionDTO data = sysRolePermissionService.get(id);

        return new WebResult<SysRolePermissionDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation(apiType = "sysrolepermission", apiPath = "sys/sysrolepermission")
    @RequiresPermissions("sys:sysrolepermission:save")
    public WebResult save(@RequestBody SysRolePermissionDTO dto) {
        //效验数据

        sysRolePermissionService.save(dto);

        return new WebResult();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation(apiType = "sysrolepermission", apiPath = "sys/sysrolepermission")
    @RequiresPermissions("sys:sysrolepermission:update")
    public WebResult update(@RequestBody SysRolePermissionDTO dto) {
        //效验数据

        sysRolePermissionService.update(dto);

        return new WebResult();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation(apiType = "sysrolepermission", apiPath = "sys/sysrolepermission")
    @RequiresPermissions("sys:sysrolepermission:delete")
    public WebResult delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sysRolePermissionService.delete(ids);

        return new WebResult();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation(apiType = "sysrolepermission", apiPath = "sys/sysrolepermission/export")
    @RequiresPermissions("sys:sysrolepermission:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SysRolePermissionDTO> list = sysRolePermissionService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, SysRolePermissionExcel.class);
    }
}